<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;
use AuctionCore\Other\CouponLog;

/**
 * 运营平台用代金券
 * @author       Eric Lee
 */
class BaseCoupon
{

    /**
     * 构造
     * @return   void
     */
    public function __construct()
    {
    }

    /**
     * 生成代金券
     * 返回一组代金券号。
     * [代金券类型，数量，卷面金额，有效起始日期，有效结束日期]
     * @return   array
     */
    public function gen($sets)
    {
        //生成代金券类型
        $data = array();
        $data['type_name'] = $sets['type'];
        $data['amount'] = $sets['amount'];
        $data['branch_company'] = $sets['branch_company'];
        //查重
        $coupon_type_id = M('coupon_type')->where("type_name='" . $sets['type'] . "' and amount='" . $sets['amount'] . "'")->getField('coupon_type_id');
        if (!$coupon_type_id) {
            $coupon_type_id = M('coupon_type')->add($data);
            if(!$coupon_type_id){
                return false;
            }
        }

        //生成代金券
        $data = array();
        $data['emp_id'] = $sets['emp_id'];
        $data['coupon_type_id'] = $coupon_type_id;
        $data['start_date'] = $sets['start_date'];
        $data['end_date'] = $sets['end_date'];
        $data['createtime'] = date('Y-m-d H:i:s');
        $data['branch_company'] = $sets['branch_company'];
        $data['grant_type'] = 2;
        $data['amount'] = $sets['amount'];
        $id = M('coupon')->add($data);
        if(!$id){
            return false;
        }
        $coupon_no = $id + 100000000;
        //修改
        $res = M('coupon')->where(array('coupon_id' => $id ))->save(array('coupon_no' => $coupon_no));
        if($res === false){
            return false;
        }
//        //日志
//        $log = array('emp_id' => $sets['emp_id'], 'action' => '生成代金券 类型：' . $sets['type'] . ',数量:' . $sets['num'] . ' 结果：' );
//        \AuctionCore\System\SystemLog::add($log);

        $arr = array(
            'coupon_id'=>$id,
            'coupon_type_id'=>$coupon_type_id,
            'coupon_no'=>$coupon_no,
            'coupon_type_name'=>$sets['type']
        );
        return $arr;
    }

    /**
     * 发放代金券给车商
     */
    public function assign_coupon($data)
    {
        //更改代金券状态
        $res = M('coupon')->where("coupon_id='" . $data['coupon_id'] . "'")->save(array('assign_status' => '1'));
        if($res === false){
            return false;
        }
        //记录到车商代金券表
        $info = array();
        $info['dealer_id'] = $data['dealer_id'];
        $info['coupon_id'] = $data['coupon_id'];
        $info['amount'] = $data['amount'];
        $info['coupon_no'] = $data['coupon_no'];
        $info['coupon_type_id'] = $data['coupon_type_id'];
        $info['coupon_type_name'] = $data['coupon_type_name'];
        $info['start_date'] = $data['start_date'];
        $info['end_date'] = $data['end_date'];
        $info['createtime'] = date('Y-m-d H:i:s');
        $dc_res= M('dealer_coupon')->add($info);
        if(!$dc_res){
            return false;
        }
        //记录代金券发放日志
        $log = array(
            'dealer_id' => $data['dealer_id'],
            'coupon_id' => $data['coupon_id'],
            'coupon_no' => $data['coupon_no'],
            'cost_type' => "发放",
            'occur_money' => $data['amount'],
            'order_id' => 0,
            'occur_type' => 0
        );
        $res = CouponLog::add($log);
        if($res === false){
            return false;
        }
        //日志
//        $log = array(
//            'emp_id' => $data['emp_id'],
//            'action' => '发放代金券给车商' . $data['dealer_id'] . ' 类型ID：' . $data['coupon_type_id'] . ',数量:' . $data['num'] . ' ，实际发放数量' . $key . ' 结果：' . $result);
//        \AuctionCore\System\SystemLog::add($log);

        return true;
    }

    /*
     * 获取有效代金券
     * */
    public function get_coupons($map = array(), $stat = false)
    {
        $map['c.assign_status'] = array('eq', '0');
        $map['c.end_date'] = array('egt', date('Y-m-d'));
        if ($stat) {
            $res = M('coupon')->alias('c')->where($map)->count();
        } else {
            $res = M('coupon')->alias('c')->field('c.*,ct.amount')->join(C('DB_PREFIX') . "coupon_type ct ON ct.coupon_type_id=c.coupon_type_id")
                ->where($map)->order('c.end_date')->select();
        }
        return $res;
    }

    /*
     * 获取所有代金券类型
     * @param type
     * */
    public function get_coupon_type($type = false)
    {
        if (!$type) {//默认显示全部代金卷类型
            $res = M('coupon_type')->select();
        } else {//发放代金卷下拉菜单 ，过滤掉不能发放的类型
            $map = array();
            $map['c.assign_status'] = array('eq', '0');
            $map['c.end_date'] = array('egt', date('Y-m-d'));
            $res = M('coupon_type')->alias('ct')->field("distinct(ct.type_name) as type_name,ct.amount,ct.coupon_type_id")->join(C('DB_PREFIX') . "coupon c ON c.coupon_type_id=ct.coupon_type_id")
                ->where($map)->select();
        }

        return $res;
    }

    /*
     * 获取代金券类型详情
     * */
    public function get_coupon_type_detail($type_id)
    {
        return M('coupon_type')->where("coupon_type_id='" . $type_id . "'")->find();
    }
}

?>